package com.bsbm.common.bean;

import java.util.ArrayList;
import java.util.List;
import java.io.Serializable; 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
 
/**
 * @author Ilya Shaikovsky
 */
@ManagedBean(name = "selectsBean")
@RequestScoped
public class SelectsBean implements Serializable {
    private static final String[] FRUITS = { "", "Banana", "Cranberry", "Blueberry", "Orange" };
    private static final String[] VEGETABLES = { "", "Potatoes", "Broccoli", "Garlic", "Carrot" };
    private static final String[] CITY = { "", "Kolkata", "Bishnupur" };
    private static final String[] TOWN = { "", "Sabir", "Iqbal" };
    public static String[] getTown() {
		return TOWN;
	}

	public List<SelectItem> getThirdList() {
		return thirdList;
	}

	private String currentItem = "";
	private String radio;
	
    public String getRadio() {
		return radio;
	}

	public void setRadio(String radio) {
		this.radio = radio;
	}

	private String currentType ;
    private String secondType ;
    public void setSecondType(String secondType) {
		this.secondType = secondType;
	}

	public void setThirdType(String thirdType) {
		this.thirdType = thirdType;
	}

	private String thirdType ;
    public String getSecondType() {
		return secondType;
	}

	public String getThirdType() {
		return thirdType;
	}

	private List<SelectItem> firstList = new ArrayList<SelectItem>();
    private List<SelectItem> secondList = new ArrayList<SelectItem>();
    private List<SelectItem> thirdList = new ArrayList<SelectItem>();
 
    public SelectsBean() {
        SelectItem item = new SelectItem("", "");
 
        firstList.add(item);
        item = new SelectItem("fruits", "Fruits");
        firstList.add(item);
        item = new SelectItem("vegetables", "Vegetables");
        firstList.add(item);
 
        for (int i = 0; i < FRUITS.length; i++) {
            item = new SelectItem(FRUITS[i]);
        }
    }
 
    public List<SelectItem> getFirstList() {
        return firstList;
    }
 
    public List<SelectItem> getSecondList() {
        return secondList;
    }
 
    public static String[] getFRUITS() {
        return FRUITS;
    }
 
    public static String[] getVEGETABLES() {
        return VEGETABLES;
    }
 
    public void valueChanged(ValueChangeEvent event) {
        secondList.clear();
        if (null != event.getNewValue()) {
            String[] currentItems;
 
            if (((String) event.getNewValue()).equals("fruits")) {
                currentItems = FRUITS;
            } else {
                currentItems = VEGETABLES;
            }
 
            for (int i = 0; i < currentItems.length; i++) {
                SelectItem item = new SelectItem(currentItems[i]);
 
                secondList.add(item);
            }
        }
    }
    public void secondValueChanged(ValueChangeEvent event) {
        thirdList.clear();
        if (null != event.getNewValue()) {
            String[] currentItems;
 
            if (((String) event.getNewValue()).equals("Banana")) {
                currentItems = CITY;
            } else {
                currentItems = TOWN;
            }
 
            for (int i = 0; i < currentItems.length; i++) {
                SelectItem item = new SelectItem(currentItems[i]);
 
                thirdList.add(item);
            }
        }
    }
    
    public void selectRadio(ValueChangeEvent event) {
    	
    	System.out.println("Inside select radio");
    }
    public static String[] getCity() {
		return CITY;
	}

	public String getCurrentType() {
        return currentType;
    }
 
    public void setCurrentType(String currentType) {
        this.currentType = currentType;
    }
 
    public String getCurrentItem() {
        return currentItem;
    }
 
    public void setCurrentItem(String currentItem) {
        this.currentItem = currentItem;
    }
    public String save() {
        String outCome = "error";
System.out.println("Inside save");
return outCome;
    }
}